package com.jia.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.product.entity.Brand;
import com.jia.product.entity.Category;
import com.jia.product.entity.CategoryBrandRelation;
import com.jia.product.mapper.BrandMapper;
import com.jia.product.mapper.CategoryBrandRelationMapper;
import com.jia.product.mapper.CategoryMapper;
import com.jia.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 品牌分类关联(CategoryBrandRelation)表服务实现类
 *
 * @author makejava
 * @since 2025-07-08 18:15:26
 */
@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationMapper, CategoryBrandRelation> implements CategoryBrandRelationService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public CategoryBrandRelation saveRelation(CategoryBrandRelation categoryBrandRelation) {

        Long brandId = categoryBrandRelation.getBrandId();

        Long catelogId = categoryBrandRelation.getCatelogId();

        Category category = categoryMapper.selectById(catelogId);

        Brand brand = brandMapper.selectById(brandId);

        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());

        this.baseMapper.insert(categoryBrandRelation);

        return categoryBrandRelation;
    }

    @Override
    public void updateDetailByBrand(Brand brand) {

        Long brandId = brand.getBrandId();
        String name = brand.getName();

        CategoryBrandRelation categoryBrandRelation = new CategoryBrandRelation();
        categoryBrandRelation.setBrandId(brandId);
        categoryBrandRelation.setCatelogName(name);

        UpdateWrapper<CategoryBrandRelation> updateWrapper
                = new UpdateWrapper<CategoryBrandRelation>();
        updateWrapper.eq("brand_id", brandId);

        this.update(categoryBrandRelation, updateWrapper);
    }

    @Override
    public void updateDetailByCategory(Category category) {
        Long catId = category.getCatId();
        String name = category.getName();

        CategoryBrandRelation categoryBrandRelation = new CategoryBrandRelation();
        categoryBrandRelation.setCatelogName(name);
        categoryBrandRelation.setCatelogId(catId);

        UpdateWrapper<CategoryBrandRelation> updateWrapper
                = new UpdateWrapper<CategoryBrandRelation>();
        updateWrapper.eq("catelog_id", catId);

        this.update(categoryBrandRelation, updateWrapper);

    }


}

